"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.saveDatabase = void 0;
const Member_1 = require("../../lib/Member");
async function saveDatabase(app, io) {
    const { socket } = app.locals;
    const { userId, username } = socket.handshake.query;
    if (!socket.saveCallLogged) {
        socket.saveCallLogged = function saveCallLogged(classroom, records, senderId) {
            const owner = Member_1.Members.get(userId);
            const sender = Member_1.Members.get(senderId || userId);
            const receives = [];
            for (const member of classroom.getInviteMembers()) {
                const { uri, userId, username } = member;
                if (userId !== sender.userId) {
                    receives.push({ userId, username });
                }
                if (uri) {
                    records.push(`/artemis-record/${uri}`);
                }
            }
            return owner.axios.callLogged({
                classId: classroom.classId,
                senderId: sender.userId,
                senderName: sender.username,
                receives,
                chatMode: classroom.chatMode,
                callTime: classroom.createTime,
                content: records,
            }).then(res => {
                socket.emit('保存通话记录', res);
                return res;
            });
        };
    }
    if (!socket.saveIntercomRecord) {
        socket.saveIntercomRecord = function saveIntercomRecord(classroom, uri, senderId) {
            const owner = Member_1.Members.get(userId);
            const sender = Member_1.Members.get(senderId || userId);
            socket.saveCallLogged(classroom, [uri], senderId);
            return owner.axios.intercomRecord({
                classId: classroom.classId,
                classname: classroom.classname,
                userId: sender.userId,
                username: sender.username,
                content: uri,
            }).then(res => {
                socket.emit('保存通话记录', res);
                console.debug('保存对讲记录', res);
                return res;
            });
        };
    }
    if (!socket.saveLoginRecord) {
        socket.saveLoginRecord = function __loginRecord(member) {
            const owner = Member_1.Members.get(userId);
            return owner.axios.loginRecord({
                userId,
                username,
                upperTime: member.onlineTime,
                downTime: Date.now().toString(),
            }).then(res => {
                socket.emit('添加用户在线时常记录', res);
                return res;
            });
        };
    }
}
exports.saveDatabase = saveDatabase;
